<template>
  <div>
    <div>
      <el-input
          size="small"
          class="addPosInput"
          placeholder="添加职位.."
          suffix-icon="el-icon-plus"
          @keydown.enter.native="addPisitions"
          v-model="pos.name">
      </el-input>
      <el-button
          icon="el-icon-plus"
          size="small"
          type="primary"
          @click="addPisitions">
        添加
      </el-button>
      <el-button
          size="small"
          style="margin-top: 8px"
          type="danger"
          :disabled="this.multipleSelection.length == 0"
          @click="deleteMany">
        批量删除
      </el-button>
    </div>
    <div class="posManaMain">
      <el-table
          size="small"
          :data="positions"
          stripe
          border
          style="width: 49%"
          @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="55"></el-table-column>
        <el-table-column prop="id" label="编号" width="55"></el-table-column>
        <el-table-column prop="name" label="职位" width="120">
        </el-table-column>
        <el-table-column prop="createdate" label="创建时间" width="100">
        </el-table-column>
        <el-table-column prop="enabled" label="是否启用" width="88">
          <template slot-scope="scope">
            <el-tag v-if="scope.row.enabled" type="success">已启用</el-tag>
            <el-tag v-else type="danger">未启用</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="150">
          <template slot-scope="scope">
            <el-button
                size="mini"
                type="primary"
                @click="showEditView(scope.$index, scope.row)">
              编辑
            </el-button>
            <el-button
                size="mini"
                type="danger"
                @click="handleDelete(scope.$index, scope.row)">
              删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>

    <el-dialog title="编辑职位" :visible.sync="dialogVisible" width="30%">
      <table>
        <tr>
          <td>
            <el-tag>职位名称</el-tag>
          </td>
          <td>
            <el-input
                v-model="updatePos.name"
                size="small"
                class="updatePosInput"
            ></el-input>
          </td>
        </tr>
        <tr>
          <td>
            <el-tag>是否启用</el-tag>
          </td>
          <td>
            <el-switch
                style="margin-left:6px"
                v-model="updatePos.enabled"
                active-color="#13ce66"
                inactive-color="#ff4949"
                active-text="已启用"
                inactive-text="未启用"
            >
            </el-switch>
          </td>
        </tr>
      </table>
      <span slot="footer" class="dialog-footer">
        <el-button size="small" @click="dialogVisible = false">取 消</el-button>
        <el-button size="small" type="primary" @click="doUpdate"
        >确 定</el-button
        >
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: 'Posmana',
  data() {
    return {
      pos: {
        name: '',
      },
      positions: [],
      dialogVisible: false,
      updatePos: {
        name: '',
        enabled: false
      },
      multipleSelection: [],
      test: true
    }
  },
  methods: {
    initPositions() {
      this.getRequest('/system/basic/pos/').then((resp) => {
        if (resp) {
          console.log(resp)
          this.positions = resp.data.records
        }
      })
    },
    showEditView(index, data) {
      Object.assign(this.updatePos, data)
      this.updatePos.createdate = ''
      this.dialogVisible = true
    },
    handleDelete(index, data) {
      this.$confirm('此操作将删[' + data.name + ']职位, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      })
          .then(() => {
            this.deleteRequest('/system/basic/pos/' + data.id).then((resp) => {
              if (resp) {
                this.initPositions()
              }
            })
          })
          .catch(() => {
            this.$message({
              type: 'info',
              message: '已取消删除',
            })
          })
    },
    addPisitions() {
      if (this.pos.name) {
        this.postRequest('/system/basic/pos/', this.pos).then((resp) => {
          if (resp) {
            // 刷新表格
            this.initPositions()
            // 清空输入框
            this.pos.name = ''
          }
        })
      } else {
        this.$message.error('职位名称不能为空！')
      }
    },
    doUpdate() {
      this.putRequest('/system/basic/pos/', this.updatePos).then((resp) => {
        if (resp) {
          this.initPositions()
          this.dialogVisible = false
        }
      })
    },
    handleSelectionChange(val) {
      this.multipleSelection = val
    },
    deleteMany() {
      this.$confirm(
          '此操作将删[' + this.multipleSelection.length + ']条职位, 是否继续?',
          '提示',
          {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning',
          }
      )
          .then(() => {
            let ids = '?'
            this.multipleSelection.forEach((item) => {
              ids += 'ids=' + item.id + '&'
            })
            this.deleteRequest('/system/basic/pos/' + ids).then((resp) => {
              if (resp) {
                this.initPositions()
              }
            })
          })
          .catch(() => {
            this.$message({
              type: 'info',
              message: '已取消删除',
            })
          })
    },
  },
  mounted() {
    this.initPositions()
  },
}
</script>

<style>
.addPosInput {
  width: 300px;
  margin-right: 8px;
}

.posManaMain {
  margin-top: 10px;
}

.updatePosInput {
  width: 200px;
  margin-left: 8px;
}
</style>
